<?
/**
 * Webapp Menu Classes
 */

class menuLink {

private $activeImgId = 'menu-selected';
public  $isActive    = false;
public  $linkMsg     = null;
public  $linkHref    = null;
public  $linkImage   = null;
private $jsOver      = 'doMenuMsg';
private $jsOut       = 'clearMenuMsg';

/**
 * constructor - optionally provide link properties
 * @param string href
 * @param string img
 * @param string msg
 * @param bool isActive
 */
public function __construct($href = null, $img = null, $msg = null,
                            $isActive = null) {
	if (is_string($href))   { $this->linkHref  = $href; }
	if (is_string($img))    { $this->linkImage = $img;  }
	if (is_string($msg))    { $this->linkMsg   = $msg;  }
	if (is_bool($isActive)) { $this->isActive = $isActive; }
	}

/**
 * Get the current link
 * @return string
 */
public function get() {
	if (!$this->isLink()) { return ''; }
	return '<a ' . $this->getMouseOver() . ' ' . $this->getMouseOut()
	   . ' ' . $this->getHref() . '>' . $this->getImg() . '</a>' . PHP_EOL;
	}

/**
 * Get the href markup
 */
public function getHref() {
	return 'href="' . $this->linkHref . '"';
	}

/**
 * Get the image markup
 * @return string
 */
public function getImg() {
	$h = '<img ';
	if ($this->isActive and is_string($this->activeImgId)) {
		$h .= 'id="' . $this->activeImgId . '" ';
		}
	$h .= 'src="' . $this->linkImage . '">';
	return $h;
	}

/**
 * Get the mouseover markup.
 * @return string
 */
public function getMouseOver() {
	return 'onmouseover="' . $this->jsOver . '(\'' . $this->linkMsg . '\')"';
	}

/**
 * Get the mouseout markup.
 * @return string
 */
public function getMouseOut() {
	return 'onmouseout="' . $this->jsOut . '()"';
	}

/**
 * Is the current instance a usable link?
 * @return bool
 */
public function isLink() {
	return (is_string($this->linkMsg) and is_string($this->linkHref) and
	        is_string($this->linkImage));
	}

}

/**
 * Menu Bar Class - displays menu bar and all links.
 */
class menuBar {

private $linksLeft  = array();
private $linksRight = array();
public  $msg = null;

/**
 * constructor - optionally set the text in the messages div
 * @param string msg
 */
public function __construct($msg = null) {
	if (is_string($msg)) { $this->msg = $msg; }
	}

/**
 * Add a menuLink object to the left side.
 * @param menuLink $link
 * @return bool
 */
public function addToLeft(&$link = null) {
	if (!($link instanceof menuLink)) { return false; }
	if (!$link->isLink()) { return false; }
	$this->linksLeft[] = $link;
	return true;
	}

/**
 * Add a menuLink object to the right side.
 * @param menuLink $link
 * @return bool
 */
public function addToRight(&$link = null) {
	if (!($link instanceof menuLink)) { return false; }
	if (!$link->isLink()) { return false; }
	$this->linksRight[] = $link;
	return true;
	}

/**
 * Get the menu bar and all links in it.
 */
public function get() {
	$h = array();
	$h[] = '<div id="menubar-container">';
	$h[] = '<div id="menubar-container-left">';
	foreach($this->linksLeft as &$l) {
		$h[] = trim($l->get());
		}
	$h[] = '</div> <!-- menubar-container-left -->';
	$h[] = '<div id="menubar-container-right">';
	foreach($this->linksRight as &$l) {
		$h[] = trim($l->get());
		}
	$h[] = '</div> <!-- menubar-container-right -->';
	$h[] = '</div> <!-- menubar-container -->';
	if (is_string($this->msg)) {
		$h[] = '<div id="menubar-messages">';
		$h[] = $this->msg;
		$h[] = '</div> <!-- menubar-messages -->';
		}
	return implode(PHP_EOL, $h) . PHP_EOL;
	}
}
?>
